New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adds bsd support #1
Merged
+66
−7
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
| @@ -0,0 +1,51 @@ | ||
| use std::fs::read_link; | ||
| use std::path::PathBuf; | ||
| use libc::*; | ||
| use std::ptr; | ||
|
|
||
| // http://stackoverflow.com/q/799679 | ||
| // http://stackoverflow.com/a/1024937 | ||
|
|
||
| #[cfg(target_os="netbsd")] | ||
| pub fn get_executable_path() -> Option<PathBuf> { | ||
| read_link("/proc/curproc/exe").ok() | ||
| } | ||
|
|
||
| #[cfg(target_os="dragonfly")] | ||
| pub fn get_executable_path() -> Option<PathBuf> { | ||
| read_link("/proc/curproc/file").ok() | ||
| } | ||
|
|
||
| #[cfg(target_os="freebsd")] | ||
| pub fn get_executable_path() -> Option<PathBuf> { | ||
| let mib = [CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1]; | ||
| let mut buf: Vec<u8> = Vec::with_capacity(1024); | ||
| let mut cb = 1024 as size_t; | ||
| let result = unsafe { | ||
| sysctl(mib.as_ptr(), | ||
| 4, | ||
| buf.as_mut_ptr() as *mut c_void, | ||
| &mut cb as *mut size_t, | ||
| ptr::null(), | ||
| 0) | ||
| }; | ||
|
|
||
| // FreeBSD without procfs | ||
| if result == 0 { | ||
| // Convert the string allocated on the stack to a Rust string. | ||
| let len = unsafe { strlen(buf.as_ptr() as *const i8) }; | ||
| unsafe { buf.set_len(len) }; | ||
| match String::from_utf8(buf).ok() { | ||
| Some(path) => { | ||
| let mut pb = PathBuf::new(); | ||
| pb.push(path); | ||
| Some(pb) | ||
| } | ||
| None => None, | ||
| } | ||
| } else { | ||
| // FreeBSD with procfs | ||
| read_link("/proc/curproc/file").ok() | ||
| } | ||
|
|
||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My only concern at this point, is that on NetBSD & DragonflyBSD, we're calling this sysctl that only works on FreeBSD. It seems that could have unintended consequences. I suggest we use the
cfg!()macro to do platform detection. For example:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, I'll do that once I have my different BSD environments running. FreeBSD will probably need an entirely different function since it uses either sysctl or a /proc file. Either way, I'll update the PR once I've pushed these modifications. Thanks for your feedback !